package com.wuliupai.delivery.wuliupaidelivery.activity;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.support.v4.content.LocalBroadcastManager;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.wuliupai.delivery.wuliupaidelivery.App;
import com.wuliupai.delivery.wuliupaidelivery.R;
import com.wuliupai.delivery.wuliupaidelivery.bean.PayBean;
import com.wuliupai.delivery.wuliupaidelivery.bean.WalletEntity;
import com.wuliupai.delivery.wuliupaidelivery.constants.BroadcastChannels;
import com.wuliupai.delivery.wuliupaidelivery.net.NetResultOperate;
import com.wuliupai.delivery.wuliupaidelivery.utils.JsonUtils;
import com.wuliupai.delivery.wuliupaidelivery.utils.LogUtils;
import com.wuliupai.delivery.wuliupaidelivery.utils.StringUtils;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.Extra;
import org.androidannotations.annotations.UiThread;
import org.androidannotations.annotations.ViewById;

/**
 * 充值和提现界面
 */
@EActivity(R.layout.activity_wallet_balancet_charge)
public class WalletBalancetChargeActivity extends AppBaseActivity {

    private String bankCardStr; // 发卡银行
    private LocalBroadcastManager lbm; //本地广播
    private BroadcastReceiver receiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            switch (intent.getAction()) {
                case BroadcastChannels.Channel_PayFeeSuccessFully://支付成功
                    Log.i("wuliupai", "支付成功回调");
                    String[] payFee1 = intent.getStringArrayExtra("payFee1");
                    String whichPage = intent.getStringExtra("whichPage");
                    Log.i("wuliupai", "支付成功回调---" + payFee1[0]);
                    String poundage = intent.getStringExtra("poundage");
                    if (payFee1[0].equals(PayFeeActivity.PayType_Recharge)) {
                        //TODO 跳转充值成功界面
                        WalletBalanetSuccessActivity_.intent(WalletBalancetChargeActivity.this).PagerType(0).bankCard(bankCardStr).balance(payFee1[1]).start();
                        animUtils.leftInAnimation(WalletBalancetChargeActivity.this);
                    } else if (payFee1[0].equals(PayFeeActivity.PayType_Reflect)) {
                        //TODO 跳转提现成功界面
                        WalletBalanetSuccessActivity_.intent(WalletBalancetChargeActivity.this).PagerType(1).bankCard(bankCardStr).balance(payFee1[1]).start();
                    }
                    animUtils.leftInAnimation(WalletBalancetChargeActivity.this);
                    finish();
            }
        }
    };


    @Extra("PagerType") // 界面类型 (1)绑定银行卡充值 (2)提现
            int PagerType;

    @ViewById(R.id.rl_wallet_Balancet_charge_not_back)
    RelativeLayout rl_wallet_Balancet_charge_not_back;//未绑定银行卡界面
    @ViewById(R.id.ll_wallet_Balancet_charge_already_back)
    LinearLayout ll_wallet_Balancet_charge_already_back;//绑定银行卡界面
    @ViewById(R.id.ll_wallet_Balancet_withdraw_deposit_already_back)
    LinearLayout ll_wallet_Balancet_withdraw_deposit_already_back;//提现界面

    @ViewById(R.id.et_wallet_not_bank_card_Cardholder)
    EditText et_wallet_not_bank_card_Cardholder;//充值未绑定银行卡输入金额
    @ViewById(R.id.et_wallet_already_bank_card_Cardholder)
    EditText et_wallet_already_bank_card_Cardholder;//充值绑定银行卡输入金额


    @ViewById(R.id.et_wallet_deposit_already_bank_card_Cardholder)
    EditText et_wallet_deposit_already_bank_card_Cardholder;//提现输入金额
    @ViewById(R.id.tv_my_wallet_withdraw_deposit_common_way)
    TextView tv_my_wallet_withdraw_deposit_common_way;//普通提现
    @ViewById(R.id.tv_my_wallet_withdraw_deposit_celerity_way)
    TextView tv_my_wallet_withdraw_deposit_celerity_way;//快速提现

    @ViewById(R.id.tv_wallet_deposit_already_bank_card)
    TextView tv_wallet_deposit_already_bank_card;//显示 提现到账银行卡
    private String mAes;

    @Click(R.id.btn_My_Wallet_next_balancet_charge)
    void onClickNext() {//点击下一步
        if (PagerType == 1) { // 当绑定银行卡 充值
            //TODO 充值
            String rechargeMoney = et_wallet_already_bank_card_Cardholder.getText().toString().trim();
            if (!StringUtils.isEmpty(rechargeMoney)) {
                PayFeeActivity_.intent(this)
                        .onlyWeChat(true)
                        .payBean(new PayBean(PayFeeActivity.PayType_Recharge, Long.valueOf(rechargeMoney) * 100))
                        .start();
                animUtils.popInAnimation(this);
            } else {
                App.toast("充值金额不能为空");
            }
        } else if (PagerType == 2) { // 当提现时
            //TODO 提现
            String wallet = et_wallet_deposit_already_bank_card_Cardholder.getText().toString().trim();
            if (!StringUtils.isEmpty(wallet)) {
                double doubleWallet = Double.parseDouble(wallet);
                if (doubleWallet >= 50) {
                    PayFeeActivity_.intent(this)
                            .onlyYuePay(true)
                            .payBean(new PayBean(PayFeeActivity.PayType_Reflect, Long.valueOf(wallet) * 100))
                            .start();
                    animUtils.popInAnimation(this);
                } else {
                    App.toast("最小提现金额50元");
                }
            } else {
                App.toast("提现金额不能为空");
            }
        }
    }

    @AfterViews
    void startPage() {
        InitViews(PagerType);
        initData();
    }

    @Override
    protected void onStart() {
        registerMyReceiver();
        super.onStart();
    }

    /**
     * 获取银行卡号数据
     */
    private void initData() {
        WalletEntity walletEntity = new WalletEntity(this);
        String walletEntityJson = JsonUtils.createJsonString(walletEntity);
        LogUtils.log("上传 获取用户的银行卡信息" + walletEntityJson);
        try {
            mAes = JsonUtils.aes(walletEntityJson);
        } catch (Exception e) {
            e.printStackTrace();
        }

        callNet(this, "getuserbankinfo", mAes, new NetResultOperate() {
            @Override
            public void onSuccess(String content) {
                LogUtils.log("获取用户的银行卡信息" + content);

                WalletEntity walletEntity = (WalletEntity) JsonUtils.parseJsonString(content, WalletEntity.class);
                if (!content.equals("{}")) {
                    int bnakNamePosition = Integer.parseInt(walletEntity.getBankId());
                    String[] BankCardNames = getResources().getStringArray(R.array.BankCardName);
                    String bankCardName = BankCardNames[bnakNamePosition - 1];

                    // 获取银行卡号
                    String bankCardNum = walletEntity.getCardNumber();
                    String subBackCardNum = bankCardNum.substring(bankCardNum.length() - 4, bankCardNum.length());

                    showBankCard(bankCardName, subBackCardNum);
                }
            }

            @Override
            public void onDataEmpty(String content) {
            }

            @Override
            public void onDataEnd(String content) {
            }
        });
    }

    /**
     * 显示银行卡号
     */
    private void showBankCard(String bankCardName, String subBackCardNum) {
        bankCardStr = bankCardName + " (" + subBackCardNum + ")";

        if (PagerType == 1) { //显示 充值储蓄卡

        } else if (PagerType == 2) { // 显示 提现 到账银行卡
            tv_wallet_deposit_already_bank_card.setText(bankCardStr);
        }
    }

    /**
     * 初始化界面
     */
    @UiThread
    void InitViews(int PagerType) {

        if (PagerType == 0) {// 显示未绑定银行卡充值界面
            //设置界面 title
            setDefaultTitle(this, R.string.strings_my_wallet_balancet_charge);
            rl_wallet_Balancet_charge_not_back.setVisibility(View.VISIBLE);
            ll_wallet_Balancet_charge_already_back.setVisibility(View.GONE);
            ll_wallet_Balancet_withdraw_deposit_already_back.setVisibility(View.GONE);
        } else if (PagerType == 1) { // 显示绑定银行卡充值界面
            //设置界面 title
            setDefaultTitle(this, R.string.strings_my_wallet_balancet_charge);
            rl_wallet_Balancet_charge_not_back.setVisibility(View.GONE);
            ll_wallet_Balancet_charge_already_back.setVisibility(View.VISIBLE);
            ll_wallet_Balancet_withdraw_deposit_already_back.setVisibility(View.GONE);
        } else if (PagerType == 2) { // 显示提现界面
            //设置界面 title
            setDefaultTitle(this, R.string.strings_my_wallet_withdraw_deposit);
            /*selectCelerityWay(2);//默认选择快速提现*/
            rl_wallet_Balancet_charge_not_back.setVisibility(View.GONE);
            ll_wallet_Balancet_charge_already_back.setVisibility(View.GONE);
            ll_wallet_Balancet_withdraw_deposit_already_back.setVisibility(View.VISIBLE);
        }
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        animUtils.leftOutAnimation(this);
    }

    private void registerMyReceiver() {
        lbm = LocalBroadcastManager.getInstance(this);
        IntentFilter filter = new IntentFilter();
        filter.addAction(BroadcastChannels.Channel_PayFeeSuccessFully);
        lbm.registerReceiver(receiver, filter);
    }

    @Override
    protected void onStop() {
        super.onStop();
        unregisterMyReceiver(lbm, receiver);

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }
}
